$(function () {
    $('form').parsley();
    dynamicBinding();
    getAllResponse();
});

//遍历所有Response输出到表格
function getAllResponse() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "响应名字", data: "name"},
        {title: "响应级别", data: "responseLevel"},
        {title: "事发地点", data: "occurredPosition"},
        {title: "事发原因", data: "occurredReason"},
        {title: "影响范围", data: "influenceScope"},
        {title: "事发时间", data: "occurredTime"},
        {title: "伤亡情况", data: "casualties"},
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateResponseModal'>更新</button>";
                }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = '/response/getAllResponse';
    dataTableInit(url, columns);
}

//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/response/deleteResponse');
    //为所有的update按钮绑定update事件
    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $('#toUpdateResponse'+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        $('#toUpdateResponse').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateResponse')[0]);
            data.set("id", id);
            data.set("occurredTime", Date($(" #updateOccurredTime").val()));
            updateAjax(data, '/response/updateResponse', false, false, "#updateResponseModal");
            return false;
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddResponse').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddResponse')[0]);
        data.set("occurredTime", Date($(" #addOccurredTime").val()));
        addAjax(data, '/response/addResponse', false, false, "#addResponseModal");
        return false;
    });

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addResponseModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddResponse").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateResponseModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateResponse").reset();
    });
}
